<script setup>
import { useRouter } from "vue-router";
import { computed, onMounted, onUnmounted, ref } from "vue";
import { getCityAll } from "@/services";
import useCityStore from "@/stores/city";
import { storeToRefs } from "pinia";

const searchVal = ref();
const activeVal = ref("cityGroup");
const router = useRouter();
const onCancel = () => {
  router.back();
};
// 方案一：请求数据
// getCityAll().then((res) => {
//   console.log(res.data, "uuu");
// });

// 方案二：pinia
let cityName = ref("");
const cityStore = useCityStore();
cityStore.fetchDatas();
let { allCities } = storeToRefs(cityStore);
const indexList = computed(() => {
  const list = allCities.value[activeVal.value].cities.map(
    (item) => item.group
  );
  list.unshift("#");
  return list;
});
</script>

<template>
  <div class="city top_page">
    <div class="c-search">
      <van-search v-model="searchVal" placeholder="请输入搜索关键词" @cancel="onCancel" shape="round" show-action />
    </div>
    <div class="c-tabs">
      <van-tabs v-model:active="activeVal" color="#ff9845">
        {{allCities.name}}
        <template v-for="(value, key) in allCities" :key="key">
          <van-tab :title="value.title" :name="key"></van-tab>
        </template>
      </van-tabs>
    </div>
    <div class="contents">
      <template v-for="(value, key1, index) in allCities" :key="index">
        <van-index-bar highlight-color="#ff9845" :index-list="indexList">
          <div v-show="activeVal === key1">
            <van-index-anchor index="热门" />
            <div class="list">
              <template v-for="city in value.hotCities" :key="city.cityId">
                <div class="city">{{ city.cityName }}</div>
              </template>
            </div>
            <template v-for="(item, key2, indey) in value.cities" :key="indey">
              <van-index-anchor :index="item.group" />
              <template v-for="(iten, key3, indez) in item.cities" :key="indez">
                <van-cell :title="iten.cityName" />
              </template>
            </template>
          </div>
        </van-index-bar>
      </template>
    </div>
  </div>
</template>
<style lang="less" scope>
.city {
  .c-search {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    // position: relative;
    // z-index: 9;
  }
  .c-tabs {
    margin-top: 54px;
  }
  .contents {
    height: calc(100vh - 98px);
    overflow-y: auto;
  }
  .list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    padding: 10px;
    padding-right: 25px;

    .city {
      width: 70px;
      height: 28px;
      border-radius: 14px;
      font-size: 12px;
      color: #000;
      text-align: center;
      line-height: 28px;
      background-color: #fff4ec;
      margin: 6px 0;
    }
  }
  :deep(.van-index-bar__index--active) {
    color: red !important;
  }
}
</style>